package xin.marcher.module.comment.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import xin.marcher.module.comment.converter.SkuCommentConverter;
import xin.marcher.module.comment.domain.dto.*;
import xin.marcher.module.comment.domain.entity.SkuCommentDO;
import xin.marcher.module.comment.domain.entity.SkuWaitCommentDO;
import xin.marcher.module.comment.domain.request.*;
import xin.marcher.module.comment.mapper.SkuCommentMapper;
import xin.marcher.module.comment.mapper.SkuWaitCommentMapper;
import xin.marcher.module.common.constants.comment.CommentConstants;
import xin.marcher.module.common.core.PageResult;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 *
 */
@Component
public class SkuCommentRepository {

    @Autowired
    private SkuWaitCommentMapper skuWaitCommentMapper;

    @Autowired
    private SkuCommentMapper skuCommentMapper;

    @Autowired
    private SkuCommentConverter skuCommentConverter;

    /**
     * 保存待评价信息
     */
    public SkuWaitCommentResultDTO saveWaitComment(SkuWaitCommentRequst request) {
        SkuWaitCommentDO entityDO = skuCommentConverter.convertWaitComment(request);
        entityDO.initCommon();
        // 初始是未完成状态
        entityDO.setFinishedFlag(false);
        skuWaitCommentMapper.insert(entityDO);
        return new SkuWaitCommentResultDTO(true);
    }

    /**
     * 查询用户的商品待评价列表
     */
    public PageResult<SkuWaitCommentDTO> listWaitCommentPage(QuerySkuWaitCommentPageRequest request) {
        LambdaQueryWrapper<SkuWaitCommentDO> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(SkuWaitCommentDO::getUserId, request.getUserId());
        // 查询的是未完成的待评价
        queryWrapper.eq(SkuWaitCommentDO::getFinishedFlag, false);
        Page<SkuWaitCommentDO> page = new Page<>(request.getPageNum(), request.getPageSize());
        Page<SkuWaitCommentDO> pageResult = skuWaitCommentMapper.selectPage(page, queryWrapper);
        return skuCommentConverter.convertWaitCommentPage(pageResult);
    }

    /**
     * 商品评价
     */
    public void saveComment(SkuCommentRequest request, SkuWaitCommentDTO skuWaitCommentDTO) {
        SkuCommentDO entityDO = skuCommentConverter.convertComment(request);
        entityDO.initCommon();
        entityDO.setOrderNo(skuWaitCommentDTO.getOrderNo());
        entityDO.setSellerId(skuWaitCommentDTO.getSellerId());
        entityDO.setItemId(skuWaitCommentDTO.getItemId());
        entityDO.setSkuId(skuWaitCommentDTO.getSkuId());
        entityDO.setUserId(skuWaitCommentDTO.getUserId());
        entityDO.setSkuScoreComputed(entityDO.getSkuScore() == CommentConstants.SCORE_ORDINARY ?
                CommentConstants.SCORE_COMPUTED_ORDINARY : (entityDO.getSkuScore() > CommentConstants.SCORE_ORDINARY ?
                CommentConstants.SCORE_COMPUTED_GOOD : CommentConstants.SCORE_COMPUTED_BAD));
        // 未审核状态
        entityDO.setFirstAuditStatus(CommentConstants.AUDIT_STATUS_WAIT);
        entityDO.setBaseTime(skuWaitCommentDTO.getBaseTime());
        // 首评时间
        entityDO.setFirstTime(new Date());
        // 非系统默认评价
        entityDO.setSysCommentFlag(false);
        skuCommentMapper.insert(entityDO);
    }

    /**
     * 根据id查询待评价信息
     */
    public SkuWaitCommentDTO getById(Long waitCommentId) {
        SkuWaitCommentDO skuWaitCommentDO = skuWaitCommentMapper.selectById(waitCommentId);
        return skuCommentConverter.convertWaitCommentDTO(skuWaitCommentDO);
    }

    /**
     * 把待评价信息修改为已完成
     */
    public void finishWaitComment(List<Long> waitCommentIds) {
        LambdaQueryWrapper<SkuWaitCommentDO> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.in(SkuWaitCommentDO::getId, waitCommentIds);

        SkuWaitCommentDO entityDO = new SkuWaitCommentDO();
        entityDO.setFinishedFlag(true);
        skuWaitCommentMapper.update(entityDO, queryWrapper);
    }

    /**
     * 商品追评
     */
    public AdditionalSkuCommentResultDTO saveAddtionalComment(SkuAddtionalCommentRequst request) {
        LambdaQueryWrapper<SkuCommentDO> queryWrapper = Wrappers.lambdaQuery();
        // 180天内可追评
        queryWrapper.eq(SkuCommentDO::getId, request.getCommentId());
        queryWrapper.gt(SkuCommentDO::getBaseTime, DateUtils.addDays(new Date(), -180));

        SkuCommentDO entityDO = new SkuCommentDO();
        entityDO.setAdditionalText(request.getAdditionalText());
        entityDO.setAdditionalUrl(request.getAdditionalUrl());
        entityDO.setAdditionalTime(new Date());
        entityDO.setAdditionalAuditStatus(CommentConstants.AUDIT_STATUS_WAIT);
        int update = skuCommentMapper.update(entityDO, queryWrapper);
        if (update > 0) {
            return new AdditionalSkuCommentResultDTO(Boolean.TRUE);
        }
        return new AdditionalSkuCommentResultDTO(Boolean.FALSE);
    }

    /**
     * 查询用户的商品评价列表
     */
    public PageResult<SkuCommentDTO> listUserCommentPage(QueryUserCommentPageRequest request) {
        LambdaQueryWrapper<SkuCommentDO> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(SkuCommentDO::getUserId, request.getUserId());
        Page<SkuCommentDO> page = new Page<>(request.getPageNum(), request.getPageSize());
        Page<SkuCommentDO> pageResult = skuCommentMapper.selectPage(page, queryWrapper);
        return skuCommentConverter.convertCommentPage(pageResult);
    }

    /**
     * 查询商品的商品评价列表
     */
    public PageResult<SkuCommentDTO> listSkuCommentPage(QuerySkuCommentPageRequest request) {
        LambdaQueryWrapper<SkuCommentDO> queryWrapper = Wrappers.lambdaQuery();
        if (StringUtils.isNotBlank(request.getItemId())) {
            queryWrapper.eq(SkuCommentDO::getItemId, request.getItemId());
        }
        if (StringUtils.isNotBlank(request.getSkuId())) {
            queryWrapper.eq(SkuCommentDO::getSkuId, request.getSkuId());
        }
        if (Objects.nonNull(request.getSkuScoreComputed()) && request.getSkuScoreComputed() > 0) {
            queryWrapper.eq(SkuCommentDO::getSkuScoreComputed, request.getSkuScoreComputed());
        }
        queryWrapper.and(wrapper -> wrapper.and(
                w -> w.eq(SkuCommentDO::getFirstAuditStatus, CommentConstants.AUDIT_STATUS_PASS)
                        .isNull(SkuCommentDO::getAdditionalAuditStatus)
        ).or(
                w -> w.eq(SkuCommentDO::getFirstAuditStatus, CommentConstants.AUDIT_STATUS_PASS)
                        .eq(SkuCommentDO::getAdditionalAuditStatus, CommentConstants.AUDIT_STATUS_WAIT)
        ).or(
                w -> w.eq(SkuCommentDO::getFirstAuditStatus, CommentConstants.AUDIT_STATUS_PASS)
                        .eq(SkuCommentDO::getAdditionalAuditStatus, CommentConstants.AUDIT_STATUS_PASS)
        ));

        queryWrapper.or();

        Page<SkuCommentDO> page = new Page<>(request.getPageNum(), request.getPageSize());
        Page<SkuCommentDO> pageResult = skuCommentMapper.selectPage(page, queryWrapper);
        return skuCommentConverter.convertCommentPage(pageResult);
    }

    /**
     * 审核初评和追评
     */
    public AuditResultDTO auditComment(AuditCommentRequest request) {
        SkuCommentDO entityDO = new SkuCommentDO();
        entityDO.setId(request.getCommentId());
        entityDO.setFirstAuditStatus(request.getFirstAuditStatus());
        entityDO.setAdditionalAuditStatus(request.getAdditionalAuditStatus());
        int update = skuCommentMapper.updateById(entityDO);
        if (update > 0) {
            return new AuditResultDTO(Boolean.TRUE);
        }
        return new AuditResultDTO(Boolean.FALSE);
    }

    /**
     * 批量保存系统默认评论
     */
    public void batchSave(List<SkuCommentDO> list) {
        skuCommentMapper.batchSave(list);
    }

    /**
     * @param days  查询多少天之前的
     * @param count 查询多少条
     */
    public List<SkuWaitCommentDTO> listShouldSysComment(int days, int count) {
        LambdaQueryWrapper<SkuWaitCommentDO> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.lt(SkuWaitCommentDO::getBaseTime, DateUtils.addDays(new Date(), -days));
        // 查询的是未完成的待评价
        queryWrapper.eq(SkuWaitCommentDO::getFinishedFlag, false);
        Page<SkuWaitCommentDO> page = new Page<>(1, count);
        Page<SkuWaitCommentDO> pageResult = skuWaitCommentMapper.selectPage(page, queryWrapper);
        return skuCommentConverter.convertSkuWaitCommentDTOList(pageResult.getRecords());
    }
}
